import axios from '@/utils/request'
export default {
  Product: {
    form: {
      fields: [
        'name',
        {
          property: 'category'
        },
        { property: 'isOnSale', default_value: true },
        { property: 'listOrder', default_value: 255 },
        { property: 'cover', type: 'image' },
        'description'
      ]
    },
    list: {
      query: {
        '@expands': "['entity.specifications']",
        '@order': 'entity.id|DESC'
      },
      list_filter: {
        'noneSpecification': {
          expression: 'entity.getSpecifications().count() == 0',
          label: '无规格产品',
          type: 'boolean'
        },
        'category.id': () => {
          return axios
            .get('/manage/categories', {
              params: { '@filter': `entity.getType().getSlug() matches 'PRODUCT'` }
            })
            .then(res =>
              Object.assign(
                { __label: '类型' },
                ...res.data.map(v => {
                  return { [v.id]: v.parent ? v.parent.__toString + ':' + v.name : v.name }
                })
              )
            )
        },
        'name': '名称'
      },
      list_display: [
        'id',
        'name',
        { property: 'cover', type: 'image' },
        { property: 'isOnSale', editable: true, label: '上架' },
        { property: 'isHot', editable: true, label: '首页推荐' },
        {
          property: 'specifications',
          label: '规格',
          component: {
            props: ['data'],
            render(h) {
              console.log(this.data)
              return (
                <div>
                  {
                    this.data.map(v => {
                      return <p>{v.__metadata.price}/{v.__toString}</p>
                    })
                  }
                </div>
              )
            }
          }
        }
      ]
    }
  }
}
